package com.farm.kaiping.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.farm.common.constant.RedisKeyConstants;
import com.farm.common.security.SecurityUtils;
import com.farm.kaiping.entity.Pigpen;
import com.farm.kaiping.po.PigpenPO;
import com.farm.kaiping.mapper.PigpenMapper;
import com.farm.kaiping.service.PigpenService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.farm.common.util.StringRange;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 圈舍管理表(Pigpen)表服务实现类
 *
 * @author makejava
 * @since 2025-01-22 13:58:17
 */
@Service
@Slf4j
@AllArgsConstructor
public class PigpenServiceImpl extends ServiceImpl<PigpenMapper, Pigpen> implements PigpenService {

	private final RedisTemplate<String,Object> redisTemplate;

	private LambdaQueryWrapper<Pigpen> generateQueryWrapper(PigpenPO pigpenPO){
		LambdaQueryWrapper<Pigpen> queryWrapper = new LambdaQueryWrapper<>();
		// 圈舍id
		queryWrapper.eq(ObjectUtil.isNotEmpty(pigpenPO.getId()), Pigpen::getId, pigpenPO.getId());
		queryWrapper.in(ObjectUtil.isNotEmpty(pigpenPO.getIdList()), Pigpen::getId, pigpenPO.getIdList());
		// 厂房id
		queryWrapper.eq(ObjectUtil.isNotEmpty(pigpenPO.getBuildingId()), Pigpen::getBuildingId, pigpenPO.getBuildingId());
		// 栏位编号
		queryWrapper.like(ObjectUtil.isNotEmpty(pigpenPO.getPigpenNo()), Pigpen::getPigpenNo, pigpenPO.getPigpenNo());
		// 占地面积(单位米)
		queryWrapper.eq(ObjectUtil.isNotEmpty(pigpenPO.getFloorArea()), Pigpen::getFloorArea, pigpenPO.getFloorArea());
		// 删除标记
		queryWrapper.eq(ObjectUtil.isNotEmpty(pigpenPO.getDelFlag()), Pigpen::getDelFlag, pigpenPO.getDelFlag());
        queryWrapper.eq(ObjectUtil.isEmpty(pigpenPO.getDelFlag()), Pigpen::getDelFlag, 0);
		// 创建人id
		queryWrapper.eq(ObjectUtil.isNotEmpty(pigpenPO.getCreateUserId()), Pigpen::getCreateUserId, pigpenPO.getCreateUserId());
		// 创建人
		queryWrapper.like(ObjectUtil.isNotEmpty(pigpenPO.getCreateUserName()), Pigpen::getCreateUserName, pigpenPO.getCreateUserName());
		// 创建时间
		queryWrapper.eq(ObjectUtil.isNotEmpty(pigpenPO.getCreateTime()), Pigpen::getCreateTime, pigpenPO.getCreateTime());
		// 更新时间
		queryWrapper.eq(ObjectUtil.isNotEmpty(pigpenPO.getUpdateTime()), Pigpen::getUpdateTime, pigpenPO.getUpdateTime());
		// 更新人
		queryWrapper.like(ObjectUtil.isNotEmpty(pigpenPO.getUpdateUserName()), Pigpen::getUpdateUserName, pigpenPO.getUpdateUserName());
		// 更新人id
		queryWrapper.eq(ObjectUtil.isNotEmpty(pigpenPO.getUpdateUserId()), Pigpen::getUpdateUserId, pigpenPO.getUpdateUserId());
		// 租户id
		queryWrapper.eq(ObjectUtil.isNotEmpty(pigpenPO.getTenantId()), Pigpen::getTenantId, pigpenPO.getTenantId());
		// 创建时间时间区间查询
		queryWrapper.ge(ObjectUtil.isNotEmpty(StringRange.getOne(pigpenPO.getCreateTimeBetween(),0)), Pigpen::getCreateTime,StringRange.getOne(pigpenPO.getCreateTimeBetween(),0));
		queryWrapper.le(ObjectUtil.isNotEmpty(StringRange.getOne(pigpenPO.getCreateTimeBetween(),1)), Pigpen::getCreateTime,StringRange.getOne(pigpenPO.getCreateTimeBetween(),1));
		// 更新时间时间区间查询
		queryWrapper.ge(ObjectUtil.isNotEmpty(StringRange.getOne(pigpenPO.getUpdateTimeBetween(),0)), Pigpen::getUpdateTime,StringRange.getOne(pigpenPO.getUpdateTimeBetween(),0));
		queryWrapper.le(ObjectUtil.isNotEmpty(StringRange.getOne(pigpenPO.getUpdateTimeBetween(),1)), Pigpen::getUpdateTime,StringRange.getOne(pigpenPO.getUpdateTimeBetween(),1));

		// buildingId集合查询
		queryWrapper.in(ObjectUtil.isNotEmpty(pigpenPO.getBuildingIdList()), Pigpen::getBuildingId, pigpenPO.getBuildingIdList());

		queryWrapper.orderByDesc(Pigpen::getCreateTime,Pigpen::getId);
		return queryWrapper;
	}

	 /**
	  * @describe 分页查询圈舍管理表
	  * @author makejava
	  * @date 2025-01-22 13:58:17
	  */
	@Override
	public Page<Pigpen> getPigpenPage(Page page, PigpenPO pigpenPO){
		LambdaQueryWrapper<Pigpen> queryWrapper = this.generateQueryWrapper(pigpenPO);
        return this.baseMapper.selectPage(page, queryWrapper);
	}
	
	/**
     * @describe 查询圈舍管理表list
     * @author  makejava
     * @date  2025-01-22 13:58:17
     */
	@Override
	public List<Pigpen> getPigpenList(PigpenPO pigpenPO){
		LambdaQueryWrapper<Pigpen> queryWrapper = this.generateQueryWrapper(pigpenPO);
        return this.baseMapper.selectList(queryWrapper);
	}
	
	/**
     * @describe 获取一条数据 圈舍管理表
     * @author  makejava
     * @date  2025-01-22 13:58:17
     */
	@Override
	public Pigpen getPigpen(PigpenPO pigpenPO){
		LambdaQueryWrapper<Pigpen> queryWrapper = this.generateQueryWrapper(pigpenPO);
        queryWrapper.last("LIMIT 1");
        return this.baseMapper.selectOne(queryWrapper);
	}

    /**
     * @describe 新增圈舍管理表
     * @author  makejava
     * @date  2025-01-22 13:58:17
     */
	@Override
	public Integer savePigpen(PigpenPO pigpenPO) {
		if(ObjectUtil.isEmpty(pigpenPO.getBuildingId())){
			throw new RuntimeException("厂房id为空");
		}
		if(ObjectUtil.isEmpty(pigpenPO.getPigpenNo())){
			throw new RuntimeException("圈舍编号为空");
		}

		int count = this.count(new LambdaQueryWrapper<Pigpen>().eq(Pigpen::getDelFlag, 0).eq(Pigpen::getBuildingId, pigpenPO.getBuildingId()).eq(Pigpen::getPigpenNo, pigpenPO.getPigpenNo()));
		if (count>0){
			throw new RuntimeException("编号重复");
		}

		pigpenPO.setDelFlag(0);
		pigpenPO.setTenantId(SecurityUtils.getUser().getTenantId());// 租户ID
		pigpenPO.setCreateTime(LocalDateTime.now());
		pigpenPO.setCreateUserId(SecurityUtils.getUser().getUserId());// 创建人
		pigpenPO.setCreateUserName(SecurityUtils.getUser().getRealName());// 创建人
		pigpenPO.setUpdateTime(LocalDateTime.now());
		Pigpen pigpen = BeanUtil.copyProperties(pigpenPO, Pigpen.class);
		this.save(pigpen);

		redisTemplate.opsForValue().set(RedisKeyConstants.PIGPEN_NO+pigpen.getId(),pigpen.getPigpenNo());

		return pigpen.getId();
	}

	/**
     * @describe 修改圈舍管理表
     * @author  makejava
     * @date  2025-01-22 13:58:17
     */
	@Override
	public String updatePigpen(PigpenPO pigpenPO) {
		if (ObjectUtil.isEmpty(pigpenPO.getId())) {
			throw new RuntimeException("id为空");
		}

		if ((ObjectUtil.isNotEmpty(pigpenPO.getPigpenNo()))) {
			int count = this.count(new LambdaQueryWrapper<Pigpen>().ne(Pigpen::getId, pigpenPO.getId()).eq(Pigpen::getDelFlag, 0).eq(Pigpen::getBuildingId, pigpenPO.getBuildingId()).eq(Pigpen::getPigpenNo, pigpenPO.getPigpenNo()));
			if (count > 0) {
				throw new RuntimeException("编号重复");
			}
		}

		pigpenPO.setUpdateUserId(SecurityUtils.getUser().getUserId());// 更新人
		pigpenPO.setUpdateUserName(SecurityUtils.getUser().getRealName());// 更新人
		pigpenPO.setUpdateTime(LocalDateTime.now());
		Pigpen pigpen = BeanUtil.copyProperties(pigpenPO, Pigpen.class);
		this.updateById(pigpen);

		redisTemplate.opsForValue().set(RedisKeyConstants.PIGPEN_NO+pigpen.getId(),pigpen.getPigpenNo());

		return "修改成功";
	}

	/**
     * @describe 通过id删除圈舍管理表
     * @author  makejava
     * @date  2025-01-22 13:58:17
     */
	@Override
	public String removePigpen(PigpenPO pigpenPO) {
		if (ObjectUtil.isEmpty(pigpenPO.getBatchDel())) {
			throw new RuntimeException("删除数据未选择");
		}
		LambdaUpdateWrapper<Pigpen> removeWrapper = new LambdaUpdateWrapper<>();
		removeWrapper.in(Pigpen::getId, pigpenPO.getBatchDel());
		removeWrapper.set(Pigpen::getDelFlag,1);
		removeWrapper.set(Pigpen::getUpdateUserId,SecurityUtils.getUser().getUserId());// 更新人
		removeWrapper.set(Pigpen::getUpdateUserName,SecurityUtils.getUser().getRealName());// 更新人
		removeWrapper.set(Pigpen::getUpdateTime,LocalDateTime.now());

		this.update(removeWrapper);

		redisTemplate.delete(pigpenPO.getBatchDel().stream().map(x -> RedisKeyConstants.PIGPEN_NO+x).collect(Collectors.toList()));

		return "删除成功";
	}
	
}

